﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 

class LinksApp extends BaseApp
{
    function index()
    {
		
		$GLOBALS['Lang']->_get_lang('links');	
		
		$TB_links = &$this->_model('links');  
		
		$linksdata = $TB_links->show();
				
        $this->assign('linksdata',$linksdata);		
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('links_list.view.html');
    }

    function edit()
    {
		
	    $id = $_GET['id'];	
		
		//$GLOBALS['Lang']->_get_lang('links');	
		
		$TB_links = &$this->_model('links');  
		
		$picdata = $TB_links->oneshow($id);
        $this->assign('picdata',$picdata);		
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('links_edit.view.html');
    }

    function add()
    {
		
	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('links_add.view.html');
    }


    function addpost()
    {

	     $data = $_POST['data'];
		 unset($data['path']);
		 
	     include(ROOT_PATH.'/lib/upfile.lib.php');	  
	     $upfile = new Upfile('links'); 	  
	     $updata = $upfile->_batch_upfile('links');		 
		 if(!isset($updata[1]['path']))	
		 {
		 $updata[1]['path'] ='';
		 }			 		 
		 $data['path'] = $updata[1]['path'];		 
	 		 
		 $TB_links = &$this->_model('links');  
	 
		
		 $TB_links->post($data);
		 
		 $this->write_cache();


		 
		 $this->index();

    }

    function editpost()
    {

	     $data = $_POST['data'];
		 unset($data['path']);
		 
		 $id = intval($data['id']);	 
		 $TB_links = &$this->_model('links');  		 		 		
		 $picdata = $TB_links->oneshow($id);
		 
		 
	     include(ROOT_PATH.'/lib/upfile.lib.php');	  
	     $upfile = new Upfile('links'); 	  
	     $updata = $upfile->_batch_upfile('links');
		 
		 if($picdata['images_dir'] && $updata)
		 {
		 $pic_dir = ROOT_PATH . '/' . UPFILE_PATH . '/' . $picdata['images_dir'];
		 del_file($pic_dir);			 
		 }
		 		 

		 
		 if(empty($updata))
		 {
		 $data['path'] = $picdata['images_dir'];		 
		 }
		 else
		 {
		 $data['path'] = $updata[1]['path'];		 
		 }
		
		 $TB_links->editaction($id,$data);
		 
		 $this->write_cache();
		 
		 $this->index();

    }


    function del()
    {

	     $id = $_GET['id'];
		 $TB_links = &$this->_model('links');  
		 		
		 $linksdata = $TB_links->oneshow($id);
		 
		 $pic_dir = ROOT_PATH . '/' . UPFILE_PATH . '/' . $linksdata['images_dir'];
		 del_file($pic_dir);
		 
		 $TB_links->del($id);	
		 	 
		 $this->write_cache();		 
		 $this->index();	  

    }



    function lsort()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['sort'] = $_GET['v'];
		 
		 $TB_links = &$this->_model('links');  	 		
		 $picdata = $TB_links->editsort($id,$data); 
		 $this->write_cache();		 
		 $this->index();			  

    }

    function lstatus()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['status'] = $_GET['s'];
		 if($data['status']==1)
		 {
		 $data['status'] = 0;
		 }
		 else
		 {
		 $data['status'] = 1;		 
		 }
		 
		 $TB_links = &$this->_model('links');  	 		
		 $picdata = $TB_links->editstatus($id,$data); 
		 $this->write_cache();		 
		 $this->index();			  

    }

    function lname()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['name'] = $_GET['v'];
		 
		 $TB_links = &$this->_model('links');  	 		
		 $picdata = $TB_links->editname($id,$data); 
		 $this->write_cache();		 
		 $this->index();			  

    }
	
    function cache()
    {
         
		 $this->write_cache();		 
		 $this->index();		 


    }	

    function write_cache()
    {
         
		 
		$TB_links = &$this->_model('links');  			
		$picdata = $TB_links->show();
		
		array_var_sort($picdata,'sort');
		
		foreach($picdata as $key => $value)
		{
		 unset($picdata[$key]['id'],$picdata[$key]['sort'],$picdata[$key]['add_time']);		  
		
		}
		
		$cache_filename = ROOT_PATH . '/data/cache/links_'.trim($GLOBALS['Lang']->_lang_tp).'.php';
		
	    write_file($cache_filename, "<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \nreturn " . var_export($picdata , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }


    }


	
}


?>